package com.hd.hdxgddcommon.utils;



import com.hd.hdxgddcommon.custom.QrHttpHandler;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.UUID;


/**
 * Created by Administrator on 2018/11/13.
 */
public class QrUtils {

    public static File createQRCode(HttpSession session, String qrCodeUrl) {

        ServletContext servletContext = session.getServletContext();
        String contextPath = servletContext.getRealPath(File.separator);

        //储存位置
        String cp = new StringBuffer(contextPath).append(contextPath.endsWith(File.separator) ? "" : File.separator).append("qr").toString();

        File file = new File(cp);

        if (!file.isDirectory()) {
            file.mkdirs();
        }
        StringBuffer completePathSb = new StringBuffer(cp).append(File.separator).append(UUID.randomUUID().toString()).append(".jpg");
        String completePath = completePathSb.toString();
        if (QrHttpHandler.download(completePath, qrCodeUrl.toString())) {
            return new File(completePath);
        } else {
            return null;
        }

//        String[] jpgPaths = new String[length];
//        int index = 0;
//        for (String ss : set) {
//            jpgPaths[index++] = ss;
//        }

        // String filename = new StringBuffer().append(new Date().getTime()).append(".zip").toString();

        //   String zipPath = new StringBuffer(cp).append(File.separator).append(filename).toString();


        //  writeZip(jpgPaths, zipPath);

        //下载文件路径
        //File file = new File(zipPath);
//        HttpHeaders headers = new HttpHeaders();
//        //下载显示的文件名，解决中文名称乱码问题
//        String downloadFielName = new String(filename.getBytes("UTF-8"), "iso-8859-1");
//        //通知浏览器以attachment（下载方式）打开zip
//        headers.setContentDispositionFormData("attachment", downloadFielName);
//        //application/octet-stream ： 二进制流数据（最常见的文件下载）。
//        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//
//        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
//                headers, HttpStatus.CREATED);
    }

    protected String idToCiphertext(String value) {
        return null;
    }


//    private void writeZip(String[] files, String zipname) throws IOException {
//        System.out.println(files);
//        OutputStream os = new BufferedOutputStream(new FileOutputStream(zipname));
//        ZipOutputStream zos = new ZipOutputStream(os);
//        byte[] buf = new byte[8192];
//        int len;
//        for (int i = 0; i < files.length; i++) {
//            File file = new File(files[i]);
//            if (!file.isFile()) continue;
//            ZipEntry ze = new ZipEntry(file.getName());
//            zos.putNextEntry(ze);
//            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//            while ((len = bis.READ(buf)) > 0) {
//                zos.write(buf, 0, len);
//            }
//            zos.closeEntry();
//            bis.close();
//        }
////        zos.setEncoding("GBK");
//        zos.closeEntry();
//        zos.close();
//
//        for (int i = 0; i < files.length; i++) {
//            File file = new File(files[i]);
//            boolean delete = file.delete();
//            System.out.println(files[i] + "------------" + delete);
//        }
//    }


}
